#coding:utf-8
import pymysql
import json
def list_to_json(listData):
    list_json = []
    #for elem in listData:
    list_json.append(listData)
    dataJson = json.dumps(list_json,ensure_ascii=False)
    return dataJson
def read(data):
    conn = pymysql.connect("localhost", "root", "1234","moyan",charset="utf8")
    print("连接成功")
    cursor = conn.cursor()
    #sql = "SELECT * FROM historydata %d WHERE name='电视' and id = %d ORDER BY id DESC LIMIT 1 "
    cursor.execute("SELECT * FROM historydata  WHERE name='空调' and id_id = %d ORDER BY id DESC LIMIT 1 " % data)
    data = cursor.fetchall()
    dic = {}
    for elem in data:
        #print(elem)
        dic['id'] = elem[1]
        dic['name'] = elem[2]
        dic['power'] = elem[3]
        dic['state'] = elem[4]
    #print(dic)
    print(list_to_json(dic),type(list_to_json(dic)))


    conn.close()
def main():
    read(156)
if __name__ == "__main__":
    main()
